package com.summer.shopping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.table.Banner;
import com.summer.shopping.domain.table.dto.BannerDto;
import com.summer.shopping.domain.table.dto.BannerQueryParam;
import com.summer.shopping.domain.table.dto.PageDTO;
import com.summer.shopping.domain.table.vo.BannerVo;
import com.summer.shopping.mapper.BannerMapper;
import com.summer.shopping.service.BannerService;
import com.summer.shopping.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
        implements BannerService {

    @Autowired
    BannerMapper bannerMapper;
    @Override
    public void add(BannerDto bannerDto) {
        Banner banner=new Banner();
        BeanUtils.copyProperties(bannerDto,banner);
        banner.setCreateTime(LocalDateTime.now());
        banner.setUpdateTime(LocalDateTime.now());
        bannerMapper.insert(banner);
    }

    @Override
    public PageResult<BannerVo> getPageList(PageDTO pageDTO, BannerQueryParam bannerQueryParam) {
        IPage<Banner> page=new Page<>(pageDTO.getPageNo(),pageDTO.getPageSize());

        QueryWrapper<Banner> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderBy(true,false,"sort_order");

        if (BeanUtil.isNotEmpty(bannerQueryParam)){
            if (bannerQueryParam.getTitle()!=null){
                queryWrapper.like("title",bannerQueryParam.getTitle());
            }
            if (bannerQueryParam.getIsVisible()!=null){
                queryWrapper.eq("is_visible",bannerQueryParam.getIsVisible());
            }
        }

        IPage<Banner> bannerIPage = bannerMapper.selectPage(page, queryWrapper);
        List<Banner> bannerList = bannerIPage.getRecords();
        List<BannerVo> voList=new ArrayList<>();
        for (Banner banner : bannerList) {
            BannerVo bannerVo = new BannerVo();
            BeanUtils.copyProperties(banner,bannerVo);
            voList.add(bannerVo);
        }

        PageResult<BannerVo> bannerVoPageResult=new PageResult<>();
        bannerVoPageResult.setRecords(voList);
        bannerVoPageResult.setTotal(bannerIPage.getTotal());
        return bannerVoPageResult;
    }

    @Override
    public BannerVo getOneById(Integer id) {
        QueryWrapper<Banner> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Banner banner = bannerMapper.selectOne(queryWrapper);
        BannerVo bannerVo=new BannerVo();
        BeanUtils.copyProperties(banner,bannerVo);
        return bannerVo;
    }

    @Override
    public void delete(List<Long> ids) {
        bannerMapper.deleteBatchIds(ids);
    }

    @Override
    public void up(BannerDto bannerDto) {
        Banner banner=new Banner();
        BeanUtils.copyProperties(bannerDto,banner);
        banner.setUpdateTime(LocalDateTime.now());
        QueryWrapper<Banner> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",bannerDto.getId());
        bannerMapper.update(banner,queryWrapper);
    }
}
